﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViCode_LeVi.Controls.Charts;
using System.Collections;
using ViCode_LeVi.Controls.Logs;
using ViCode_LeVi.Data;
using System.Collections.Specialized;
using ViCode_LeVi.Controls.BTBData;

namespace ViCode_LeVi.Controls.Reports
{
    public class ReportDataSourceHelper
    {
        private string GetActionStateString(ActionState eActionState)
        {
            switch (eActionState)
            {
                case ActionState.PLANNED:
                    return "in Planung";

                case ActionState.INPROGRESS:
                    return "in Bearbeitung";

                case ActionState.FINISHED:
                    return "abgeschlossen";
            }
            return string.Empty;
        }

        //private FactorList GetFactorsByPeriod(Project oWorkingProject, PeriodList aPeriodList)
        //{
        //    return oWorkingProject.FactorsByPeriod(aPeriodList, new FactorType[] { FactorType.HK, FactorType.SK, FactorType.BK });
        //}

        //private FactorList GetFactorsByPeriod(Project oWorkingProject, PeriodList aPeriodList, bool bSortByFactor)
        //{
        //    FactorList list = null;
        //    if ((aPeriodList != null) && (oWorkingProject != null))
        //    {
        //        list = new FactorList();
        //        if (bSortByFactor)
        //        {
        //            list.Add(oWorkingProject.FactorsByPeriod(aPeriodList, new FactorType[] { FactorType.HK }));
        //            list.Add(oWorkingProject.FactorsByPeriod(aPeriodList, new FactorType[] { FactorType.SK }));
        //            list.Add(oWorkingProject.FactorsByPeriod(aPeriodList, new FactorType[] { FactorType.BK }));
        //            return list;
        //        }
        //        list.Add(oWorkingProject.FactorsByPeriod(aPeriodList, new FactorType[] { FactorType.HK, FactorType.SK, FactorType.BK }));
        //    }
        //    return list;
        //}

        public ReportActionItem[] GetReportActionList()
        {
            return ProjectData.Intance.Period.GetReportActionList();
        }

        public ReportFactorItem[] GetReportFactorList(FactorType eEFType)
        {
            return ProjectData.Intance.Period.GetReportFactorList(eEFType);
        }

        public ReportImportanceStatementItem[] GetReportImportanceStatementList(FactorType eEFType)
        {
            return ProjectData.Intance.Period.GetReportImportanceStatementList(eEFType);
        }

        public ReportImportanceTimeSeriesItem[] GetReportImportanceTimeSeriesList(PeriodList aPeriodList)
        {
            List<ReportImportanceTimeSeriesItem> list = new List<ReportImportanceTimeSeriesItem>();
            //ReportImportanceTimeSeriesItem item = null;
            //FactorList list2 = null;
            //try
            //{
            //    if (oWorkingProject == null)
            //    {
            //        return list;
            //    }
            //    list2 = this.GetFactorsByPeriod(oWorkingProject, aPeriodList, true);
            //    if (list2 == null)
            //    {
            //        return list;
            //    }
            //    foreach (Factor factor in list2)
            //    {
            //        item = new ReportImportanceTimeSeriesItem
            //        {
            //            ID = factor.LogicalID,
            //            FactorName = this.GetString(factor.Name)
            //        };
            //        switch (aPeriodList.Count)
            //        {
            //            case 1:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) != null)
            //                {
            //                    item.ImportancePeriode3 = factor.ExtensionForPeriod((Period)aPeriodList[0]).Importance.ToString();
            //                }
            //                break;

            //            case 2:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) != null)
            //                {
            //                    item.ImportancePeriode2 = factor.ExtensionForPeriod((Period)aPeriodList[0]).Importance.ToString();
            //                }
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[1]) != null)
            //                {
            //                    item.ImportancePeriode3 = factor.ExtensionForPeriod((Period)aPeriodList[1]).Importance.ToString();
            //                }
            //                break;

            //            case 3:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) != null)
            //                {
            //                    item.ImportancePeriode1 = factor.ExtensionForPeriod((Period)aPeriodList[0]).Importance.ToString();
            //                }
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[1]) != null)
            //                {
            //                    item.ImportancePeriode2 = factor.ExtensionForPeriod((Period)aPeriodList[1]).Importance.ToString();
            //                }
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[2]) != null)
            //                {
            //                    item.ImportancePeriode3 = factor.ExtensionForPeriod((Period)aPeriodList[2]).Importance.ToString();
            //                }
            //                break;
            //        }
            //        list.Add(item);
            //    }
            //}
            //catch (Exception exception)
            //{
            //    Log.LogException(exception);
            //}
            return list.ToArray();
        }

        public ReportQQSStatementItem[] GetReportQQSStatementList(FactorType eEFType)
        {
            return ProjectData.Intance.Period.GetReportQQSStatementList(eEFType);           
        }        
        public ReportQQSTimeSeriesItem[] GetReportQQSTimeSeriesList(PeriodList aPeriodList)
        {
            List<ReportQQSTimeSeriesItem> list = new List<ReportQQSTimeSeriesItem>();
            //ReportQQSTimeSeriesItem item = null;
            //FactorList list2 = null;
            //try
            //{
            //    if (oWorkingProject == null)
            //    {
            //        return list;
            //    }
            //    list2 = this.GetFactorsByPeriod(oWorkingProject, aPeriodList, true);
            //    if (list2 == null)
            //    {
            //        return list;
            //    }
            //    foreach (Factor factor in list2)
            //    {
            //        QQSRecord qQS;
            //        QQSRecord record2;
            //        QQSRecord record4;
            //        item = new ReportQQSTimeSeriesItem
            //        {
            //            ID = factor.LogicalID,
            //            FactorName = this.GetString(factor.Name)
            //        };
            //        switch (aPeriodList.Count)
            //        {
            //            case 1:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) == null)
            //                {
            //                    goto Label_0600;
            //                }
            //                qQS = factor.ExtensionForPeriod((Period)aPeriodList[0]).QQS;
            //                if (qQS == null)
            //                {
            //                    goto Label_0600;
            //                }
            //                if (!qQS.QuantityUsed || (qQS.QuantityAsIs == DH.INVALID_INT))
            //                {
            //                    break;
            //                }
            //                item.Qn3 = qQS.QuantityAsIs.ToString("0");
            //                goto Label_00F2;

            //            case 2:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) == null)
            //                {
            //                    goto Label_0258;
            //                }
            //                record2 = factor.ExtensionForPeriod((Period)aPeriodList[0]).QQS;
            //                if (record2 == null)
            //                {
            //                    goto Label_0258;
            //                }
            //                if (!record2.QuantityUsed || (record2.QuantityAsIs == DH.INVALID_INT))
            //                {
            //                    goto Label_01D7;
            //                }
            //                item.Qn2 = record2.QuantityAsIs.ToString("0");
            //                goto Label_01E2;

            //            case 3:
            //                if (factor.ExtensionForPeriod((Period)aPeriodList[0]) == null)
            //                {
            //                    goto Label_0430;
            //                }
            //                record4 = factor.ExtensionForPeriod((Period)aPeriodList[0]).QQS;
            //                if (record4 == null)
            //                {
            //                    goto Label_0430;
            //                }
            //                if (!record4.QuantityUsed || (record4.QuantityAsIs == DH.INVALID_INT))
            //                {
            //                    goto Label_03AF;
            //                }
            //                item.Qn1 = record4.QuantityAsIs.ToString("0");
            //                goto Label_03BA;

            //            default:
            //                goto Label_0600;
            //        }
            //        item.Qn3 = "-";
            //    Label_00F2:
            //        if (qQS.QualityUsed && (qQS.QualityAsIs != DH.INVALID_INT))
            //        {
            //            item.Ql3 = qQS.QualityAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Ql3 = "-";
            //        }
            //        if (qQS.SystematicsUsed && (qQS.SystematicsAsIs != DH.INVALID_INT))
            //        {
            //            item.Sy3 = qQS.SystematicsAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Sy3 = "-";
            //        }
            //        goto Label_0600;
            //    Label_01D7:
            //        item.Qn2 = "-";
            //    Label_01E2:
            //        if (record2.QualityUsed && (record2.QualityAsIs != DH.INVALID_INT))
            //        {
            //            item.Ql2 = record2.QualityAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Ql2 = "-";
            //        }
            //        if (record2.SystematicsUsed && (record2.SystematicsAsIs != DH.INVALID_INT))
            //        {
            //            item.Sy2 = record2.SystematicsAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Sy2 = "-";
            //        }
            //    Label_0258:
            //        if (factor.ExtensionForPeriod((Period)aPeriodList[1]) != null)
            //        {
            //            QQSRecord record3 = factor.ExtensionForPeriod((Period)aPeriodList[1]).QQS;
            //            if (record3 != null)
            //            {
            //                if (record3.QuantityUsed && (record3.QuantityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Qn3 = record3.QuantityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Qn3 = "-";
            //                }
            //                if (record3.QualityUsed && (record3.QualityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Ql3 = record3.QualityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Ql3 = "-";
            //                }
            //                if (record3.SystematicsUsed && (record3.SystematicsAsIs != DH.INVALID_INT))
            //                {
            //                    item.Sy3 = record3.SystematicsAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Sy3 = "-";
            //                }
            //            }
            //        }
            //        goto Label_0600;
            //    Label_03AF:
            //        item.Qn1 = "-";
            //    Label_03BA:
            //        if (record4.QualityUsed && (record4.QualityAsIs != DH.INVALID_INT))
            //        {
            //            item.Ql1 = record4.QualityAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Ql1 = "-";
            //        }
            //        if (record4.SystematicsUsed && (record4.SystematicsAsIs != DH.INVALID_INT))
            //        {
            //            item.Sy1 = record4.SystematicsAsIs.ToString("0");
            //        }
            //        else
            //        {
            //            item.Sy1 = "-";
            //        }
            //    Label_0430:
            //        if (factor.ExtensionForPeriod((Period)aPeriodList[1]) != null)
            //        {
            //            QQSRecord record5 = factor.ExtensionForPeriod((Period)aPeriodList[1]).QQS;
            //            if (record5 != null)
            //            {
            //                if (record5.QuantityUsed && (record5.QuantityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Qn2 = record5.QuantityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Qn2 = "-";
            //                }
            //                if (record5.QualityUsed && (record5.QualityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Ql2 = record5.QualityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Ql2 = "-";
            //                }
            //                if (record5.SystematicsUsed && (record5.SystematicsAsIs != DH.INVALID_INT))
            //                {
            //                    item.Sy2 = record5.SystematicsAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Sy2 = "-";
            //                }
            //            }
            //        }
            //        if (factor.ExtensionForPeriod((Period)aPeriodList[2]) != null)
            //        {
            //            QQSRecord record6 = factor.ExtensionForPeriod((Period)aPeriodList[2]).QQS;
            //            if (record6 != null)
            //            {
            //                if (record6.QuantityUsed && (record6.QuantityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Qn3 = record6.QuantityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Qn3 = "-";
            //                }
            //                if (record6.QualityUsed && (record6.QualityAsIs != DH.INVALID_INT))
            //                {
            //                    item.Ql3 = record6.QualityAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Ql3 = "-";
            //                }
            //                if (record6.SystematicsUsed && (record6.SystematicsAsIs != DH.INVALID_INT))
            //                {
            //                    item.Sy3 = record6.SystematicsAsIs.ToString("0");
            //                }
            //                else
            //                {
            //                    item.Sy3 = "-";
            //                }
            //            }
            //        }
            //    Label_0600:
            //        list.Add(item);
            //    }
            //}
            //catch (Exception exception)
            //{
            //    Log.LogException(exception);
            //}
            return list.ToArray();
        }

        public ReportQuestionAnswerItem[] GetReportQuestionAnswerList(int iStep, int iPart)
        {
            iStep--;
            iPart--;
            try
            {
                return ProjectData.Intance.Period.GetReportQuestionAnswerList(iStep, iPart);
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
                return new ReportQuestionAnswerItem[0];
            }
        }

        private string GetString(string sString)
        {
            if ((sString != null) && (sString.Length > 1))
            {
                while (sString.Substring(sString.Length - 2, 2) == "\r\n")
                {
                    sString = sString.Substring(0, sString.Length - 2);
                }
            }
            return sString;
        }
    }
    public class ReportQuestionAnswerItem
    {
        private string m_sAnswer = string.Empty;
        private string m_sQuestion = string.Empty;

        public string Answer
        {
            get
            {
                return this.m_sAnswer;
            }
            set
            {
                this.m_sAnswer = value;
            }
        }

        public string Question
        {
            get
            {
                return this.m_sQuestion;
            }
            set
            {
                this.m_sQuestion = value;
            }
        }
    }
    public class ReportImportanceStatementItem
    {
        private string m_sID = string.Empty;
        private string m_sStatement = string.Empty;

        public string ID
        {
            get
            {
                return this.m_sID;
            }
            set
            {
                this.m_sID = value;
            }
        }

        public string Statement
        {
            get
            {
                return this.m_sStatement;
            }
            set
            {
                this.m_sStatement = value;
            }
        }
    }
    public class ReportFactorItem
    {
        private string m_sDefinition = string.Empty;
        private string m_sFactorName = string.Empty;
        private string m_sID = string.Empty;

        public string Definition
        {
            get
            {
                return this.m_sDefinition;
            }
            set
            {
                this.m_sDefinition = value;
            }
        }

        public string FactorName
        {
            get
            {
                return this.m_sFactorName;
            }
            set
            {
                this.m_sFactorName = value;
            }
        }

        public string ID
        {
            get
            {
                return this.m_sID;
            }
            set
            {
                this.m_sID = value;
            }
        }
    }
    public class ReportQQSStatementItem
    {
        private string m_sID = string.Empty;
        private string m_sStatementQl = string.Empty;
        private string m_sStatementQn = string.Empty;
        private string m_sStatementSy = string.Empty;

        public string ID
        {
            get
            {
                return this.m_sID;
            }
            set
            {
                this.m_sID = value;
            }
        }

        public string StatementQl
        {
            get
            {
                return this.m_sStatementQl;
            }
            set
            {
                this.m_sStatementQl = value;
            }
        }

        public string StatementQn
        {
            get
            {
                return this.m_sStatementQn;
            }
            set
            {
                this.m_sStatementQn = value;
            }
        }

        public string StatementSy
        {
            get
            {
                return this.m_sStatementSy;
            }
            set
            {
                this.m_sStatementSy = value;
            }
        }
    }
    public class ReportActionItem
    {
        private string m_sBenefit = string.Empty;
        private string m_sDuration = string.Empty;
        private string m_sEffort = string.Empty;
        private string m_sName = string.Empty;
        private string m_sProcedure = string.Empty;
        private string m_sResult = string.Empty;
        private string m_sStartDate = string.Empty;
        private string m_sState = string.Empty;

        public string Benefit
        {
            get
            {
                return this.m_sBenefit;
            }
            set
            {
                this.m_sBenefit = value;
            }
        }

        public string Duration
        {
            get
            {
                return this.m_sDuration;
            }
            set
            {
                this.m_sDuration = value;
            }
        }

        public string Effort
        {
            get
            {
                return this.m_sEffort;
            }
            set
            {
                this.m_sEffort = value;
            }
        }

        public string Name
        {
            get
            {
                return this.m_sName;
            }
            set
            {
                this.m_sName = value;
            }
        }

        public string Procedure
        {
            get
            {
                return this.m_sProcedure;
            }
            set
            {
                this.m_sProcedure = value;
            }
        }

        public string Result
        {
            get
            {
                return this.m_sResult;
            }
            set
            {
                this.m_sResult = value;
            }
        }

        public string StartDate
        {
            get
            {
                return this.m_sStartDate;
            }
            set
            {
                this.m_sStartDate = value;
            }
        }

        public string State
        {
            get
            {
                return this.m_sState;
            }
            set
            {
                this.m_sState = value;
            }
        }
    }
    public class ReportQQSTimeSeriesItem
    {
        private string m_sFactorName = string.Empty;
        private string m_sID = string.Empty;
        private string m_sQl1 = string.Empty;
        private string m_sQl2 = string.Empty;
        private string m_sQl3 = string.Empty;
        private string m_sQn1 = string.Empty;
        private string m_sQn2 = string.Empty;
        private string m_sQn3 = string.Empty;
        private string m_sSy1 = string.Empty;
        private string m_sSy2 = string.Empty;
        private string m_sSy3 = string.Empty;

        public string FactorName
        {
            get
            {
                return this.m_sFactorName;
            }
            set
            {
                this.m_sFactorName = value;
            }
        }

        public string ID
        {
            get
            {
                return this.m_sID;
            }
            set
            {
                this.m_sID = value;
            }
        }

        public string Ql1
        {
            get
            {
                return this.m_sQl1;
            }
            set
            {
                this.m_sQl1 = value;
            }
        }

        public string Ql2
        {
            get
            {
                return this.m_sQl2;
            }
            set
            {
                this.m_sQl2 = value;
            }
        }

        public string Ql3
        {
            get
            {
                return this.m_sQl3;
            }
            set
            {
                this.m_sQl3 = value;
            }
        }

        public string Qn1
        {
            get
            {
                return this.m_sQn1;
            }
            set
            {
                this.m_sQn1 = value;
            }
        }

        public string Qn2
        {
            get
            {
                return this.m_sQn2;
            }
            set
            {
                this.m_sQn2 = value;
            }
        }

        public string Qn3
        {
            get
            {
                return this.m_sQn3;
            }
            set
            {
                this.m_sQn3 = value;
            }
        }

        public string Sy1
        {
            get
            {
                return this.m_sSy1;
            }
            set
            {
                this.m_sSy1 = value;
            }
        }

        public string Sy2
        {
            get
            {
                return this.m_sSy2;
            }
            set
            {
                this.m_sSy2 = value;
            }
        }

        public string Sy3
        {
            get
            {
                return this.m_sSy3;
            }
            set
            {
                this.m_sSy3 = value;
            }
        }
    }
    public class ReportImportanceTimeSeriesItem
    {
        private string m_sFactorName = string.Empty;
        private string m_sID = string.Empty;
        private string m_sImportancePeriode1 = string.Empty;
        private string m_sImportancePeriode2 = string.Empty;
        private string m_sImportancePeriode3 = string.Empty;

        public string FactorName
        {
            get
            {
                return this.m_sFactorName;
            }
            set
            {
                this.m_sFactorName = value;
            }
        }

        public string ID
        {
            get
            {
                return this.m_sID;
            }
            set
            {
                this.m_sID = value;
            }
        }

        public string ImportancePeriode1
        {
            get
            {
                return this.m_sImportancePeriode1;
            }
            set
            {
                this.m_sImportancePeriode1 = value;
            }
        }

        public string ImportancePeriode2
        {
            get
            {
                return this.m_sImportancePeriode2;
            }
            set
            {
                this.m_sImportancePeriode2 = value;
            }
        }

        public string ImportancePeriode3
        {
            get
            {
                return this.m_sImportancePeriode3;
            }
            set
            {
                this.m_sImportancePeriode3 = value;
            }
        }
    }
}
